home *** CD-ROM | disk | FTP | other *** search
/ EnigmA Amiga Run 1995 October / EnigmA AMIGA RUN 01 (1995)(G.R. Edizioni)(IT)[!][issue 1995-10][Aminet 7].iso / Aminet / comm / net / amitcp2_x_gcc.lha / RCS.RCSfiles / lineread.c,v < prev    next >
Text File  |  1994-01-12  |  5KB  |  241 lines

  1. head    1.2;
  2. access;
  3. symbols;
  4. locks
  5.     jasegler:1.2; strict;
  6. comment    @ * @;
  7.  
  8.  
  9. 1.2
  10. date    94.01.12.18.23.14;    author jasegler;    state Exp;
  11. branches;
  12. next    1.1;
  13.  
  14. 1.1
  15. date    94.01.11.19.02.22;    author jasegler;    state Exp;
  16. branches;
  17. next    ;
  18.  
  19.  
  20. desc
  21. @@
  22.  
  23.  
  24. 1.2
  25. log
  26. @*** empty log message ***
  27. @
  28. text
  29. @/*
  30.  * lineread.c
  31.  *
  32.  * Author: Tomi Ollila <too@@cs.hut.fi>
  33.  *
  34.  * This module is FREEWARE. Standard "NO WARRANTY" disclaimer applies.
  35.  *
  36.  * Created: Thu Jul 12 16:40:03 EET 1990 too
  37.  * Last modified: Tue Jul 13 18:05:49 1993 too
  38.  *
  39.  * $Id: lineread.c,v 1.1 1994/01/11 19:02:22 jasegler Exp $
  40.  *
  41.  * HISTORY
  42.  * $Log: lineread.c,v $
  43.  * Revision 1.1  1994/01/11  19:02:22  jasegler
  44.  * Initial revision
  45.  *
  46.  * Revision 1.1  1994/01/11  19:02:22  jasegler
  47.  * Initial revision
  48.  *
  49.  * Revision 1.3  1993/07/13  15:15:53  too
  50.  * Made lineread.c compile without <sys/cdefs.h> file
  51.  *
  52.  * Revision 1.1  1993/06/16  16:43:52  too
  53.  * Initial revision
  54.  *
  55.  *
  56.  */
  57.  
  58. #include "lineread.h"
  59.  
  60. #ifdef AMIGA
  61. extern struct Library *SocketBase;
  62. #define READ(a, b, c) recv(a, b, c, 0)
  63. #if __SASC
  64. #include <proto/socket.h>
  65. #elif __GNUC__
  66. #include <clib/socket_protos.h>
  67. #endif
  68. #else /* not AMIGA */
  69. #define READ(a, b, c) read(a, b, c)
  70. #endif /* AMIGA */
  71.  
  72. #ifndef NULL
  73. #define NULL 0
  74. #endif
  75.  
  76. #ifndef FALSE
  77. #define FALSE 0
  78. #endif
  79.  
  80. #ifndef TRUE
  81. #define TRUE 1
  82. #endif
  83.  
  84. #ifndef __CONCAT
  85. #if defined (__STDC__) || defined (__cplusplus)
  86. #define __CONCAT(x,y) x ## y
  87. #else
  88. #define __CONCAT(x,y) x/**/y
  89. #endif
  90. #endif /* __CONCAT not defined */
  91.  
  92. #define RLP(field) __CONCAT(rl->rl_Private.rlp_,field)
  93.  
  94.  
  95. #if defined (__STDC__) || defined (__cplusplus)
  96.  
  97. int
  98. lineRead (struct LineRead *rl)
  99.  
  100. #else
  101.  
  102. int
  103. lineRead (rl)
  104.      struct LineRead *rl;
  105.  
  106. #endif
  107. {
  108.   int i;
  109.  
  110.   if (RLP (Bufpointer) == RLP (Howlong))
  111.  
  112.     if (RLP (Selected))
  113.       {
  114.  
  115.     if (RLP (Line_completed))
  116.       RLP (Startp) = RLP (Bufpointer) = 0;
  117.  
  118.     if ((i = READ (rl->rl_Fd,
  119.                RLP (Buffer) + RLP (Bufpointer),
  120.                RLP (Buffersize) - RLP (Bufpointer))) <= 0)
  121.       {
  122.         /*
  123.            * here if end-of-file or on error. set Howlong == Bufpointer
  124.            * so if non-blocking I/O is in use next call will go to READ()
  125.          */
  126.         RLP (Howlong) = RLP (Bufpointer);
  127.         rl->rl_Line = NULL;
  128.         return i;
  129.       }
  130.     else
  131.       RLP (Howlong) = RLP (Bufpointer) + i;
  132.       }
  133.     else
  134.       /* Inform user that next call may block (unless select()ed) */
  135.       {
  136.     RLP (Selected) = TRUE;
  137.     return 0;
  138.       }
  139.   else
  140.     /* Bufpointer has not reached Howlong yet. */
  141.     {
  142.       RLP (Buffer)[RLP (Bufpointer)] = RLP (Saved);
  143.       RLP (Startp) = RLP (Bufpointer);
  144.     }
  145.  
  146.   /*
  147.    * Scan read string for next newline.
  148.    */
  149.   while (RLP (Bufpointer) < RLP (Howlong))
  150.     if (RLP (Buffer)[RLP (Bufpointer)++] == '\n')
  151.       goto Skip;
  152.  
  153.   /*
  154.    * Here if Bufpointer == Howlong.
  155.    */
  156.   if (rl->rl_Lftype != RL_LFNOTREQ)
  157.     {
  158.       RLP (Selected) = TRUE;
  159.  
  160.       if (RLP (Bufpointer) == RLP (Buffersize))
  161.     {
  162.       /*
  163.          * Here if Bufpointer reaches end-of-buffer.
  164.        */
  165.       if (RLP (Startp) == 0)
  166.         {            /* (buffer too short for whole string) */
  167.           RLP (Line_completed) = TRUE;
  168.           rl->rl_Line = RLP (Buffer);
  169.           RLP (Buffer)[RLP (Bufpointer)] = '\0';
  170.           return -1;
  171.         }
  172.       /*
  173.          * Copy partial string to start-of-buffer and make control ready for
  174.          * filling rest of buffer when next call to lineRead() is made
  175.          * (perhaps after select()).
  176.        */
  177.       for (i = 0; i < RLP (Buffersize) - RLP (Startp); i++)
  178.         RLP (Buffer)[i] = RLP (Buffer)[RLP (Startp) + i];
  179.       RLP (Howlong) -= RLP (Startp);
  180.       RLP (Bufpointer) = RLP (Howlong);
  181.       RLP (Startp) = 0;
  182.     }
  183.  
  184.       RLP (Line_completed) = FALSE;
  185.       return 0;
  186.     }
  187.  
  188. Skip:
  189.   RLP (Line_completed) = TRUE;
  190.   if (rl->rl_Lftype == RL_LFREQNUL)
  191.     RLP (Buffer)[RLP (Bufpointer) - 1] = '\0';
  192.   RLP (Saved) = RLP (Buffer)[RLP (Bufpointer)];
  193.   RLP (Buffer)[RLP (Bufpointer)] = '\0';
  194.   RLP (Selected) = FALSE;
  195.   rl->rl_Line = RLP (Buffer) + RLP (Startp);
  196.  
  197.   return (RLP (Bufpointer) - RLP (Startp));
  198. }
  199.  
  200. #undef READ
  201.  
  202. #if defined (__STDC__) || defined (__cplusplus)
  203.  
  204. void
  205. initLineRead (struct LineRead *rl, int fd, int lftype, int buffersize)
  206.  
  207. #else
  208.  
  209. int
  210. initLineRead (rl, fd, lftype, buffersize)
  211.      struct LineRead *rl;
  212.      int fd;
  213.      int lftype;
  214.      int buffersize;
  215.  
  216. #endif
  217. {
  218.   rl->rl_Fd = fd;
  219.   rl->rl_Lftype = lftype;
  220.  
  221.   RLP (Bufpointer) = RLP (Howlong) = 0;
  222.   RLP (Selected) = RLP (Line_completed) = TRUE;
  223.  
  224.   RLP (Buffersize) = buffersize;
  225. }
  226. @
  227.  
  228.  
  229. 1.1
  230. log
  231. @Initial revision
  232. @
  233. text
  234. @d11 1
  235. a11 1
  236.  * $Id: lineread.c,v 1.3 1993/07/13 15:15:53 too Exp $
  237. d15 6
  238. a38 1
  239. #include <inline/socket.h>
  240. @
  241.